const net = require('net')
const port = 89
const host = 'localhost'
const client = net.createConnection({
    port,
    host
})

const TransformCode = require('./TransformCode')
const transformer = new TransformCode()
const arrs = ['xm0', 'xm1', 'xm2', 'xm3', 'xm4', '']
client.on('connect', () => {
    arrs.forEach((item) => {
        client.write(transformer.encode(item))
    })
    client.end()
})

let buffer =  null
let len = 0
client.on('data', (chunk) => {
    if(buffer){
        chunk = Buffer.concat([buffer,chunk])
     }
     while(len = transformer.getLen(chunk)){
        const content = chunk.slice(0,len)
        chunk = chunk.slice(len)
        const res = transformer.decode(content)
        console.log(res)
     }
     buffer = chunk
})
client.on('error', (err) => {
    console.log(err)
})
// client.on('close', () => {
//     console.log('close')
// })
